<template>
    <BPTable ref='tableRef' align='center' row-key='id' :columns='columns' :api='pageApi' :buttons='buttons' :rowSelection="false">
        
    </BPTable>

    <BDialog v-model:visible='visible' :title='dialogTitle' width='600px' :confirm='submit' @close="resetForm">
        <a-form ref='formRef' layout="vertical" :rules="rules" :label-col='{span : 5}' :model='formData'>
            <a-form-item style='display:none' name='id'>
                <a-input type='hidden' v-model:value='formData.id' />
            </a-form-item>
            <a-form-item :label="$c('name')" name="name">
                <BLangSelect v-model:value='formData.name' />
            </a-form-item>
            <a-form-item label="key" name="key">
                <a-input v-model:value='formData.key' />
            </a-form-item>
            <a-form-item :label="$c('remark')" name="remark">
                <a-input v-model:value='formData.remark' />
            </a-form-item>
            <a-form-item :label="$t('function')" name="func">
                <Codemirror v-model:value="formData.func" name="func" height="200" @change="changeFunc" :options="{mode : 'text/javascript', theme : '3024-night'}"></Codemirror>
            </a-form-item>
            
        </a-form>
    </BDialog>

</template>

<script lang='ts' setup>
    import { pageApi, deleteApi, saveApi } from '@/api/devices/DeviceDataField'
    import { useI18nStore } from '@/store/i18n'
    import { message, Modal } from 'ant-design-vue'
    import { ref } from 'vue'
    import { Rule } from 'ant-design-vue/lib/form';

    import "codemirror/mode/javascript/javascript.js"
    import "codemirror/theme/3024-night.css"
    import Codemirror from "codemirror-editor-vue3"
    


    const i18n = useI18nStore()
    const visible = ref(false)

    const tableRef = ref()
    const formRef = ref()
    const dialogTitle = ref()
    

    const formData = ref({
        id : 0,
        name : '',
        key : '',
        remark : '',
        func : ''

    })

    function changeFunc(e) {
        formData.value.func = e
    }

    
    const rules: Record<string, Rule[]> = {
        name : [
            {
                required : true,
                message : i18n.concat('please', 'input', 'name')
            }
        ],

        key : [
            {
                required : true,
                message : i18n.concat('please', 'input', 'key')
            }
        ],
    };

    

    const buttons = ref<SearchButton[]>([])
    buttons.value.push({
        button : 'add',
        func : () => {
            dialogTitle.value = i18n.t('add')
            formData.value.func = ''
            visible.value = true
        }
    })


  
    function resetForm() {
        formRef.value.clearValidate()
        formRef.value.resetFields()
    }
    
    async function submit() {
        let { id, ...params} = await formRef.value.validate()
        
        if(id) {
            params.id = Number(id)
        }

        

        const res = await saveApi(params)
        if(res.success) {
            resetForm()
            visible.value = false
            message.success(i18n.t('saveSuccess'))
            tableRef.value.load()
        }else {
            Modal.error({
                title : res.msg
            })
        }
    }


    

    const columns = ref<Column[]>([
        {
            title : i18n.t('name'),
            dataIndex : 'name',
            align : 'center',
            translate : true,
            inSearch : true
        },

        {
            title : 'key',
            dataIndex : 'key',
            align : 'center',
            inSearch : true,
        },

        {
            title : i18n.t('function'),
            dataIndex : 'func',
            align : 'center',
        },

        {
            title : i18n.t('remark'),
            dataIndex : 'remark',
            align : 'center',
        },

        {
            title : i18n.t('action'),
            dataIndex : '$action',
            align : 'center',
            $edit : (row) => {
                dialogTitle.value = i18n.t('edit')
                for(let k in formData.value) {
                    formData.value[k] = row[k]
                    
                }
                // formFields.forEach(field => {
                //     if(field.name == 'expireIn') {
                //         field.defaultValue = row[field.name] ? dayjs(row[field.name] * 1000) : ''
                //     }else {
                //         field.defaultValue = row[field.name]
                //     }
                    
                // })

                visible.value = true
            },

            $delete :  async (row) => {
                const res = await deleteApi(row.id)
                if(res.success) {
                    message.success(i18n.t('deleteSuccess'))
                    tableRef.value.load()
                }else {
                    message.error(res.msg)
                }
            }
        }
    ])


    

    
</script>

<style lang='scss' module>
    .username{
        display: inline-flex;
        align-items: center;
        gap: 10px;
    }
</style>